package com.liuzu.esys.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.liuzu.esys.bo.SettlementBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.common.R;
import com.liuzu.esys.pojo.Settlement;
import com.liuzu.esys.service.SettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/settlement")
public class SettlementController {

    @Autowired
    private SettlementService settlementService;

    @RequestMapping("/settlementList")
    public LayuiPageVo<Settlement> settlementPageData(@RequestParam Map<String,Object> params){
        return settlementService.settlementPageData(params);
    }

    @RequestMapping("/addSettlement")
    public R addSettlement(Settlement settlement){
        System.out.println(settlement);
        int result = settlementService.addSettlement(settlement);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/updateSettlement")
    public R updateSettlement(Settlement settlement){

        int result = settlementService.updateSettlement(settlement);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("/deleteSettlement")
    public R deleteSettlement(Integer order_number){
        int result = settlementService.deleteSettlement(order_number);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("/deleteSettlements")
    public R deleteSettlements(String order_numbers){
        int result = settlementService.deleteSettlements(order_numbers);
        if (result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/export")
    public void export(Settlement settlement, HttpServletResponse response) throws IOException{
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        String filename = URLEncoder.encode("结算订单导出", "utf-8");

        response.setHeader("Content-Disposition","attachment;filename=" + filename + ".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, SettlementBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        List<SettlementBo> settlements = settlementService.settlementExcelData(settlement);

        sheet.doWrite(settlements);
    }

}
